package controller;

import com.github.pagehelper.PageInfo;
import constant.Constant;
import entity.Comment;
import entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import service.CommentService;
import util.AjaxResult;
import util.DateUtil;
import vo.CommentVo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;

@Controller
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    CommentService commentService;


    //加载评论
    @RequestMapping("/loadResComs.do")
    @ResponseBody
    public AjaxResult loadComment(HttpServletRequest req, CommentVo commentVo) {
        AjaxResult result = new AjaxResult(true, "success", null);

        commentVo.setPageNo(Integer.valueOf(req.getParameter("pageNo")));
        commentVo.setResource_id(1);
//        commentVo.setResource_id(Integer.valueOf(req.getParameter("resource_id")));

        PageInfo<CommentVo> info = commentService.loadComment(commentVo);

        result.setObj(info);
        return result;
    }

    /*
    前台：发表评论
    data:{"resource_id":id,"context":context}
    **/
    @RequestMapping("/sendComs.do")
    @ResponseBody
    public AjaxResult commitComment(HttpServletRequest request, HttpSession session){
        AjaxResult result = new AjaxResult(true, "发表成功，待审核中...", null);
        String context = request.getParameter("context");
//        int resource_id = Integer.parseInt(request.getParameter("resource_id"));
        int resource_id = 1;//测试数据
        User user = (User) session.getAttribute(Constant.SESSION_USER);
        Comment comment=new Comment();
//        comment.setUser_id(user.getId());
        comment.setUser_id(1);//测试数据
        comment.setContext(context);
        comment.setCreate_date(new Date());
        comment.setResource_id(resource_id);
        comment.setStatus(Constant.COMMENT_STATUS_WAITING);
        if(!commentService.insertComment(comment)){
            result.setSuccess(false);
            result.setMsg("系统正忙,请重新发表!");
            return result;
        }
        return result;
    }

    //获取赞数
    @RequestMapping("/getPraCount.do")
    @ResponseBody
    public AjaxResult getPraCount(HttpServletRequest req) {
        AjaxResult result = new AjaxResult(true, "success", null);
        Integer id = Integer.valueOf(req.getParameter("comment_id"));
        CommentVo c = new CommentVo();
        //c.setPraise_count(Integer.valueOf(commentService.getPraCount(id)));

        PageInfo<CommentVo> info = commentService.getPraCount(c);
        result.setObj(info);
        return result;
    }

    //评论管理-加载评论的方法
    @RequestMapping("/findComs.do")
    @ResponseBody
    public AjaxResult CMloadComment(HttpServletRequest req, CommentVo commentVo) {
        AjaxResult result = new AjaxResult(true, "success", null);

        commentVo.setLogin_name(String.valueOf(req.getParameter("login_name")));
        commentVo.setBegin_date(DateUtil.startDate(req.getParameter("begin_date")));
        commentVo.setEnd_date(DateUtil.startDate(req.getParameter("end_date")));
        commentVo.setContext(String.valueOf(req.getParameter("context")));
        commentVo.setPageNo(Integer.valueOf(req.getParameter("pageNo")));
        commentVo.setStatus(Integer.valueOf(req.getParameter("status")));

        PageInfo<CommentVo> info = commentService.CMloadCommeommentVo(commentVo);

        result.setObj(info);
        return result;
    }

    //审核评论，通过：0;禁用：1
    @RequestMapping("/toggle.do")
    @ResponseBody
    public AjaxResult toggle(HttpServletRequest req, CommentVo commentVo) {
        AjaxResult result = new AjaxResult(true, "success", null);

        commentVo.setId(Integer.valueOf(req.getParameter("id")));
        commentVo.setStatus(Integer.valueOf(req.getParameter("status")));

        commentService.updateCommentStatus(commentVo);

        return result;
    }

    //加载管理评论
    @RequestMapping("/findByChapterId.do")
    @ResponseBody
    public AjaxResult findByChapterId(HttpServletRequest req, CommentVo commentVo) {
        AjaxResult result = new AjaxResult(true, "success", null);

        commentVo.setLogin_name(String.valueOf(req.getParameter("login_name")));
        commentVo.setBegin_date(DateUtil.startDate(req.getParameter("begin_date")));
        commentVo.setEnd_date(DateUtil.startDate(req.getParameter("end_date")));
        commentVo.setContext(String.valueOf(req.getParameter("context")));
        commentVo.setPageNo(Integer.valueOf(req.getParameter("pageNo")));
        commentVo.setStatus(Integer.valueOf(req.getParameter("status")));
        commentVo.setChapter_id(Integer.valueOf(req.getParameter("chapter_id")));

        PageInfo<CommentVo> info = commentService.findCommentByChapterId(commentVo);

        result.setObj(info);
        return result;
    }

}
